package utils

import (
	"encoding/json"
	"fmt"
	"hanyun-admin/src/entity"
	"log"
)

//json转map
func JsonToMap(jsonStr string)(jsonMap map[string]interface{}){
	jsonMap = make(map[string]interface{},0)
	json.Unmarshal([]byte(jsonStr),&jsonMap)
	return
}
//通过用户的唯一标识获取用户信息
func GetLoginUserInfo(userName string)entity.User{
	var user entity.User
	jsonStr:=GetValueByKey("getLoginUser_"+userName)
	err := json.Unmarshal([]byte(jsonStr),&user)
	if err != nil {
		log.Println("json string to struct error",err)
	}
	return user
}
//获取用户信息
func GetLoginUser(token string)entity.User{
	var user entity.User
	jsonStr :=GetLoginUserName(token)
	userMap := JsonToMap(jsonStr)
	userName := userMap["userName"].(string)
	user = GetLoginUserInfo(userName)
	return user
}

//字符串首字母转化大写 user -> User
func Capitalize(str string)string{
	var upperStr string
	vv := []rune(str)
	for i := 0;i<len(vv);i++{
		if i==0 {
			if vv[i] >= 97 && vv[i] <=122{
				vv[i] -= 32
				upperStr += string(vv[i])
			}else {
				fmt.Println("Not begins with lowercase letter,")
				return str
			}
		}else {
			upperStr += string(vv[i])
		}
	}
	return upperStr
}